package com.comac.print.component.app.command.cmdExe;

import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.comac.print.component.app.cmd.SystemCmd;
import com.comac.print.component.app.domain.gateway.print.SystemGateway;
import com.comac.print.component.app.domain.gateway.printE.SystemE;
import com.comac.print.component.app.gatewayimpl.database.dataobject.SystemDO;
import com.comac.print.component.app.gatewayimpl.database.mapper.SystemMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class SystemCmdExe {


    @Autowired
    private SystemGateway systemGateway;

    @Autowired
    private SystemMapper systemMapper;

    public Response saveSystemInformation(SystemCmd systemCmd) {
        //查询系统校验该系统名是不是已经维护过
        QueryWrapper<SystemDO> queryWrapperSystemDO = new QueryWrapper<>();
        queryWrapperSystemDO.eq("SYSTEM_NAME", systemCmd.getSystemName());
        SystemDO systemDO = systemMapper.selectOne(queryWrapperSystemDO);
        if(systemDO != null){
            return  SingleResponse.buildFailure("-1","该系统名称已维护!");
        }
            SystemE systemE = new SystemE();
            BeanUtils.copyProperties(systemCmd,systemE);
            systemGateway.create(systemE);
        return SingleResponse.buildSuccess();
    }

    public Response systemDelete(SystemCmd systemCmd) {
        if (systemCmd.getStId() == null) {
            return SingleResponse.buildFailure("-1", "系统的id不能为空!");
        }
        systemMapper.deleteById(systemCmd.getStId());
        return SingleResponse.buildSuccess();
    }
}
